<template>
  <div class="app-container">
    <div>
      <el-button type="primary" @click="showTable()">添加商品</el-button>
    </div>
    <el-dialog title="添加商品" :visible.sync="dialogVisible" width="80%" :before-close="handleClose"
      :modal-append-to-body="false" :append-to-body="true">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="商品编号" prop="proCode">
          <el-input v-model="queryParams.proCode" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="商品名称" prop="proName">
          <el-input v-model="queryParams.proName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="分类" prop="typeId">
          <el-select v-model="queryParams.typeId" placeholder="请选择商品分类">
            <el-option :label="type.typeName" :value="type.id" v-for="type in typeList" :key="type.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="70" align="left">
          <template slot-scope="scope">
            {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
          </template>
        </el-table-column>
        <el-table-column label="商品编号" align="center" prop="proCode" />
        <el-table-column label="商品名称" align="center" prop="proName" />
        <el-table-column label="商品单位" align="center" prop="unitName" />
        <el-table-column label="商品成本" align="center" prop="cost" />
        <el-table-column label="商品销售底价" align="center" prop="price" />
        <el-table-column label="商品类别" align="center" prop="typeName" />
        <el-table-column label="商品备注" align="center" prop="proRemark" />
      </el-table>
      <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <br>
    <el-button type="primary" @click="addOrderDetail()">添加</el-button>
    </el-dialog>
  </div>
</template>
<script>
import { listGoods, checkProCodeRequest } from "@/api/basedata/goods";
import { listAllType } from "@/api/basedata/type";
import { v4 as uuidv4 } from 'uuid';
import { addDetail } from "@/api/xiaoshou/saleRequest";

export default {
  name: "AddGoods",
  props:['orderId','addGoodsToOrder'],
  data() {
      const checkProCode = (rule, value, callback) => {
      const queryParams = {
        proCode: value
      };
      checkProCodeRequest(queryParams).then(rs => {
          if (rs.code == "200") {
            callback();
          } else {
            callback(new Error('商品编码已存在'));
          }
        })
    };

    return {
      dialogVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proCode: null,
        proName: null,
        typeId: null,
      },
      addDto:{
        orderId:'',
        ids:{},
      },
      unitList: [
        { id: 1, unitName: "个" },
        { id: 2, unitName: "包" },
        { id: 3, unitName: "盒" },
      ],
      typeList: [
        { id: 1, typeName: "食品" },
        { id: 2, typeName: "衣服" },
        { id: 3, typeName: "鞋子" },
        { id: 4, typeName: "化妆品" },
        { id: 5, typeName: "其他" },
      ],
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
    this.getTypeAll();
  },
  methods: {
    addOrderDetail(){
      let selectedGoods = this.goodsList.filter(good => this.ids.includes(good.id));
      this.addGoodsToOrder(selectedGoods);
      this.dialogVisible=false;
      
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClose(done){
      done();
    },
    /** 获取所有商品分类 */
    getTypeAll() {
      listAllType().then(response => {
        this.typeList = response.data;
      });
    },
    // getUnitAll() {
    //   listAllUnit().then(response => {
    //     this.unitList = response.data;
    //   });
    showTable(){
      this.dialogVisible = true
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proCode: null,
        proName: null,
        unitId: null,
        price: null,
        typeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
};
</script>
